package com.briup.jd2407cms.web.controller;

import com.briup.jd2407cms.aop.Logging;
import com.briup.jd2407cms.domain.Comment;
import com.briup.jd2407cms.domain.DTO.CommentDeleteParam;
import com.briup.jd2407cms.domain.DTO.CommentQueryParam;
import com.briup.jd2407cms.domain.Subcomment;
import com.briup.jd2407cms.mapper.SubcommentMapper;
import com.briup.jd2407cms.service.CommentService;
import com.briup.jd2407cms.service.SubcommentService;
import com.briup.jd2407cms.util.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @BelongsProject: JD2407-cms
 * @BelongsPackage: com.briup.jd2407cms.web.controller
 * @Author: 鲨鲨天下第一
 * @CreateTime: 2024-11-19  09:06
 * @Description: TODO
 * @Version: 1.0
 */
@Api(tags = "评论模块")
@Slf4j
@RestController
@RequestMapping("/auth/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;
    @Autowired
    private SubcommentService subcommentService;

    @ApiOperation(value = "分页+条件查询", notes = "查询条件：关键字、userId、articleId、发表时间范围")
    @Logging("分页查询评论信息")
    @PostMapping("/query")
    public Result query(@RequestBody CommentQueryParam param) {
        return Result.success(commentService.page(param));
    }

    @ApiOperation(value = "新增一级评论", notes = "一级评论直接对文章进行评论")
    @Logging("评论文章")
    @PostMapping("/saveComment")
    public Result saveComment(@RequestBody Comment comment) {
        return Result.success(commentService.saveComment(comment) ? "新增一级评论成功" : "新增一级评论失败");
    }

    @ApiOperation(value = "新增二级评论", notes = "二级评论是对评论的回复")
    @Logging("回复评论")
    @PostMapping("/saveSubComment")
    public Result saveSubComment(@RequestBody Subcomment comment) {
        return Result.success(subcommentService.saveSubComment(comment) ? "回复评论成功" : "回复评论失败");
    }

    @ApiOperation(value = "根据id删除评论", notes = "type为1表示1级评论，为2表示2级评论")
    @Logging("通过id删除评论")
    @DeleteMapping("/deleteById")
    public Result deleteById(@RequestBody CommentDeleteParam param) {
        return Result.success(commentService.deleteById(param) ? "删除成功" : "");
    }

    @ApiOperation(value = "根据id批量删除评论", notes = "type为1表示1级 评论，为2表示2级评论")
    @Logging("批量删除评论")
    @DeleteMapping("/deleteByIdAll")
    public Result deleteByIdAll(@RequestBody List<CommentDeleteParam> list) {
        commentService.deleteInBatch(list);
        return Result.success("删除成功");
    }
}
